from new_test_framework.utils import tdLog, tdSql, tdStream, StreamItem
import time

class TestFunCols:
    def setup_class(cls):
        cls.replicaVar = 1  # 设置默认副本数
        tdLog.debug(f"start to excute {__file__}")
        #tdSql.init(conn.cursor(), logSql)
        cls.dbname = 'test'

    def condition_check(self, condition, row, col, expected_value):
        if condition:
            tdSql.checkData(row, col, expected_value)

    def create_test_data(self, cachemodel):       
        tdSql.execute(f'drop database if exists {self.dbname}')
        tdSql.execute(f'create database if not exists {self.dbname} cachemodel \'{cachemodel}\';')
        tdSql.execute(f'use {self.dbname}')
        tdSql.execute(f'drop table if exists {self.dbname}.meters')
        
        # tdLog.info("create test data")
        # tdLog.info("taosBenchmark -y -t 10 -n 100  -b INT,FLOAT,NCHAR,BOOL")
        # os.system("taosBenchmark -y -t 10 -n 100  -b INT,FLOAT,NCHAR,BOOL")
        
        tdSql.execute(f'create table {self.dbname}.meters (ts timestamp, c0 int, c1 float, c2 nchar(30), c3 bool) tags (t1 nchar(30))')
        tdSql.execute(f'create table {self.dbname}.d0 using {self.dbname}.meters tags("st1")')
        tdSql.execute(f'create table {self.dbname}.d1 using {self.dbname}.meters tags("st2")')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929000, 1, 1, "c2", true)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929001, 2, 2, "bbbbbbbbb1", false)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929002, 2, 2, "bbbbbbbbb1", false)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929003, 3, 3, "a2", true)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929004, 4, 4, "bbbbbbbbb2", false)')
        
        tdSql.execute(f'insert into {self.dbname}.d1 values(1734574929000, 1, 1, "c2", true)')
        
        tdSql.execute(f'use {self.dbname}')        
        tdSql.execute(f'Create table  {self.dbname}.normal_table (ts timestamp, c0 int, c1 float, c2 nchar(30), c3 bool)')
        tdSql.execute(f'insert into {self.dbname}.normal_table (select * from {self.dbname}.d0)')
        
    def one_cols_1output_test(self):
        tdLog.info("one_cols_1output_test")
        tdSql.query(f'select cols(last(ts), ts) from {self.dbname}.meters')
        tdSql.checkResColNameList(['ts'])
        tdSql.checkRows(1)
        tdSql.checkData(0, 0, "2024-12-19 10:22:09.004")
        tdSql.query(f'select cols(last(ts), ts) as t1 from {self.dbname}.meters')
        tdSql.checkResColNameList(['t1'])
        tdSql.query(f'select cols(last(ts), ts as t1) from {self.dbname}.meters')
        tdSql.checkResColNameList(['t1'])
        tdSql.query(f'select cols(last(ts), c0) from {self.dbname}.meters')
        tdSql.checkResColNameList(['c0'])
        tdSql.query(f'select cols(last(ts), c1) from {self.dbname}.meters group by tbname')
        tdSql.checkResColNameList(['c1'])
        

        tdSql.query(f'select cols(last(ts+1), ts) as t1 from {self.dbname}.meters')
        tdSql.checkResColNameList(['t1'])
        tdSql.query(f'select cols(last(ts+1), ts+2 as t1) from {self.dbname}.meters')
        tdSql.checkResColNameList(['t1'])
        tdSql.query(f'select cols(last(ts+1), c0+10) from {self.dbname}.meters')
        tdSql.checkResColNameList(['c0+10'])

        tdSql.query(f'select cols(last(c0), ts) from {self.dbname}.meters')
        tdSql.checkData(0, 0, "2024-12-19 10:22:09.004")

    def one_cols_multi_output_with_group_test(self, from_table = 'test.meters', isTmpTable = False):
        select_t1 = ["", ", t1", ", t1 as tag1"]
        for t1 in select_t1:
            tags_count = 0 if t1 == "" else 1
            tdLog.info("one_cols_1output_test_with_group")
            tdSql.query(f'select cols(last(c1), ts) {t1} from {from_table} group by tbname')
            tdSql.checkRows(2)
            tdSql.checkCols(1 + tags_count)
            tdSql.query(f'select cols(last(c1), ts) {t1} from {from_table} group by tbname order by tbname')
            tdSql.checkCols(1 + tags_count)
            tdSql.checkData(0, 0, 1734574929004)
            tdSql.checkData(1, 0, 1734574929000)
            tdSql.query(f'select cols(last(c1), ts) {t1} from {from_table} group by tbname order by ts')
            tdSql.checkCols(1 + tags_count)
            tdSql.checkData(0, 0, 1734574929000)
            tdSql.checkData(1, 0, 1734574929004)
            tdSql.query(f'select cols(last(c1), ts), tbname {t1} from {from_table} group by tbname order by tbname')
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1734574929004)
            tdSql.checkData(0, 1, 'd0')
            tdSql.checkData(1, 0, 1734574929000)
            tdSql.checkData(1, 1, 'd1')
            tdSql.query(f'select cols(last(c1), ts), tbname, t1 from {from_table} group by tbname order by tbname')
            tdSql.checkRows(2)
            tdSql.checkCols(3)
            tdSql.checkData(0, 0, 1734574929004)
            tdSql.checkData(0, 1, 'd0')
            tdSql.checkData(0, 2, 'st1')
            tdSql.checkData(1, 0, 1734574929000)
            tdSql.checkData(1, 1, 'd1')
            tdSql.checkData(1, 2, 'st2')        
            tdSql.query(f'select cols(last(c1), ts), tbname, t1 from {from_table} group by tbname order by t1')
            tdSql.checkResColNameList(['ts', 'tbname', 't1'])
            tdSql.checkRows(2)
            tdSql.checkCols(3)
            tdSql.checkData(0, 0, 1734574929004)
            tdSql.checkData(0, 1, 'd0')
            tdSql.checkData(0, 2, 'st1')
            tdSql.checkData(1, 0, 1734574929000)
            tdSql.checkData(1, 1, 'd1')
            tdSql.checkData(1, 2, 'st2')
            tdSql.query(f'select cols(last(ts), ts, c0), count(1) {t1} from {from_table} group by t1 order by t1')
            tdSql.checkRows(2)
            tdSql.checkCols(3 + tags_count)
            tdSql.checkData(0, 0, 1734574929004)
            tdSql.checkData(0, 1, 4)
            tdSql.checkData(0, 2, 5)
            self.condition_check(t1!="", 0, 3, 'st1')
            tdSql.checkData(1, 0, 1734574929000)
            tdSql.checkData(1, 1, 1)
            tdSql.checkData(1, 2, 1)
            self.condition_check(t1!="", 1, 3, 'st2')
            
            tdSql.query(f'select cols(last(ts), ts, c0), sum(c0) {t1} from {from_table} group by t1 order by t1')
            tdSql.checkRows(2)
            tdSql.checkCols(3 + tags_count)
            tdSql.checkData(0, 0, 1734574929004)
            tdSql.checkData(0, 1, 4)
            tdSql.checkData(0, 2, 12)
            self.condition_check(t1!="", 0, 3, 'st1')
            tdSql.checkData(1, 0, 1734574929000)
            tdSql.checkData(1, 1, 1)
            tdSql.checkData(1, 2, 1)
            self.condition_check(t1!="", 1, 3, 'st2') 

            tdSql.error(f'select cols(last(ts), ts, c0), count(1), t1 from {from_table} group by t1 order by tbname')

            if t1 != "" and isTmpTable:
                # Not a GROUP BY expression
                tdSql.error(f'select cols(last(ts), ts, c0), count(1) {t1} from {from_table} group by tbname order by tbname')
                tdSql.error(f'select cols(last(ts), ts, c0), count(1), tbname {t1} from {from_table} group by tbname order by tbname')
                tdSql.error(f'select cols(max(c0), ts, c0), count(1), tbname {t1} from {from_table} group by tbname order by tbname')
                tdSql.error(f'select cols(last(c1), ts), count(1) {t1} from {from_table} group by tbname')
                continue
            tdSql.query(f'select cols(last(ts), ts, c0), count(1) {t1} from {from_table} group by tbname order by tbname')
            tdSql.checkRows(2)
            tdSql.checkCols(3 + tags_count)
            tdSql.checkData(0, 0, 1734574929004)
            tdSql.checkData(0, 1, 4)
            tdSql.checkData(0, 2, 5)
            tdSql.checkData(1, 0, 1734574929000)
            tdSql.checkData(1, 1, 1)
            tdSql.checkData(1, 2, 1)       
            tdSql.query(f'select cols(last(ts), ts, c0), count(1), tbname {t1} from {from_table} group by tbname order by tbname')
            tdSql.checkRows(2)
            tdSql.checkCols(4 + tags_count)
            tdSql.checkData(0, 0, 1734574929004)
            tdSql.checkData(0, 1, 4)
            tdSql.checkData(0, 2, 5)
            tdSql.checkData(0, 3, 'd0')
            tdSql.checkData(1, 0, 1734574929000)
            tdSql.checkData(1, 1, 1)
            tdSql.checkData(1, 2, 1)
            tdSql.checkData(1, 3, 'd1')
            tdSql.query(f'select cols(max(c0), ts, c0), count(1), tbname {t1} from {from_table} group by tbname order by tbname')
            tdSql.checkRows(2)
            tdSql.checkCols(4 + tags_count)
            tdSql.checkData(0, 0, 1734574929004)
            tdSql.checkData(0, 1, 4)
            tdSql.checkData(0, 2, 5)
            tdSql.checkData(0, 3, 'd0')
            self.condition_check(t1!="", 0, 4, 'st1')
            tdSql.checkData(1, 0, 1734574929000)
            tdSql.checkData(1, 1, 1)
            tdSql.checkData(1, 2, 1)
            tdSql.checkData(1, 3, 'd1')
            self.condition_check(t1!="", 1, 4, 'st2') 

            tdSql.query(f'select cols(last(c1), ts), count(1) {t1} from {from_table} group by tbname')
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)

    def one_cols_multi_output_test(self, from_table = 'test.meters'):
        tdLog.info(f"one_cols_1output_test {from_table}")
        tdSql.query(f'select cols(last(ts), ts, c0) from {from_table}')
        tdSql.checkResColNameList(['ts', 'c0'])
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.query(f'select cols(last(ts), ts as time, c0 cc) from {from_table}')
        tdSql.checkResColNameList(['time', 'cc'])
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.query(f'select cols(last(ts), ts as t123456789t123456789t123456789t123456789t123456789t123456789t123456789, c0 cc) from {from_table}')
        tdSql.checkResColNameList(['t123456789t123456789t123456789t123456789t123456789t123456789t123', 'cc'])
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.query(f'select cols(last(ts), c0, c1, c2, c3) from {from_table}')
        tdSql.checkResColNameList(['c0', 'c1', 'c2', 'c3'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 4)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 'bbbbbbbbb2')
        tdSql.checkData(0, 3, False)
        tdSql.query(f'select cols(last(ts), c0, t1) from {from_table}')
        tdSql.checkResColNameList(['c0', 't1'])
        tdSql.checkRows(1)
        tdSql.checkData(0, 0, 4)
        tdSql.checkData(0, 1, 'st1')
        tdSql.query(f'select cols(max(c0), ts) from {from_table}')
        tdSql.checkResColNameList(['ts'])
        tdSql.checkCols(1)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.query(f'select cols(min(c1), ts, c0) from {from_table}')
        tdSql.checkResColNameList(['ts', 'c0'])
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)

        tdSql.query(f'select cols(max(c0), ts, c0), count(1) from {from_table}')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 6)
        tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {self.dbname}.d0')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 5)
        tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {self.dbname}.d1')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1)
        tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {self.dbname}.normal_table')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 5)
        tdSql.query(f'select cols(first(ts), ts, c0), count(1) from {self.dbname}.normal_table')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 5)
        tdSql.query(f'select cols(min(c0), ts, c0), count(1) from {self.dbname}.normal_table')
        tdSql.checkResColNameList(['ts', 'c0', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 5)

        tdSql.query(f'select cols(last(ts), ts, c0), avg(c0) from {from_table}')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 2.1666666666666665)

        tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {from_table}')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 6)

        tdSql.query(f'select cols(last(ts), ts, c0), sum(c0) from {from_table}')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 13)

        tdSql.query(f'select count(1), cols(last(ts), ts, c0), min(c0) from {from_table}')
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 6)
        tdSql.checkData(0, 1, 1734574929004)
        tdSql.checkData(0, 2, 4)
        tdSql.checkData(0, 3, 1)

        tdSql.query(f'select count(1), cols(last_row(ts), ts, c0), min(c0) from test.meters')
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 6)
        tdSql.checkData(0, 1, 1734574929004)
        tdSql.checkData(0, 2, 4)
        tdSql.checkData(0, 3, 1)

        tdSql.query(f'select count(1), cols(last_row(ts), ts, c0), last_row(c1), last_row(c3) from test.meters')
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 6)
        tdSql.checkData(0, 1, 1734574929004)
        tdSql.checkData(0, 2, 4)
        tdSql.checkData(0, 3, 4)
        tdSql.checkData(0, 4, False)


        tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {from_table}')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 6)
        
        tdSql.query(f'select cols(max(c0), ts, c0), count(1) from {from_table}')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 6)   

        tdSql.query(f'select cols(last(ts), ts as time, c0 cc), count(1) from {from_table}')
        tdSql.checkResColNameList(['time', 'cc', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 6)

        tdSql.query(f'select cols(max(c1), ts as time, c0 cc), count(1) from {from_table}')
        tdSql.checkResColNameList(['time', 'cc', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 6) 

        tdSql.query(f'select cols(last(ts), c0, c1, c2, c3), count(1) from {from_table}')
        tdSql.checkResColNameList(['c0', 'c1', 'c2', 'c3', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 4)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 'bbbbbbbbb2')
        tdSql.checkData(0, 3, False)
        tdSql.checkData(0, 4, 6)      

        tdSql.query(f'select cols(max(c0), ts), count(1) from {from_table}')
        tdSql.query(f'select cols(min(c1), ts, c0), count(1) from {from_table}')
        tdSql.query(f'select count(1), cols(max(c0), ts) from {from_table}')
        tdSql.query(f'select max(c0), cols(max(c0), ts) from {from_table}')
        tdSql.query(f'select max(c1), cols(max(c0), ts) from {from_table}')

    def multi_cols_output_test(self, from_table = 'test.meters', isTmpTable = False):
        tdLog.info("multi_cols_output_test")
        tdSql.query(f'select cols(last(c0), ts, c1), cols(first(c0), ts, c1), count(1) from {self.dbname}.meters')
        tdSql.checkResColNameList(['ts', 'c1', 'ts', 'c1', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 6)
        tdSql.query(f'select cols(last(c0),ts lts, c1 lc1), cols(first(c0), ts fts, c1 as fc1), count(1) from test.meters')
        tdSql.checkResColNameList(['lts', 'lc1', 'fts', 'fc1', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 6)
        tdSql.query(f'select cols(max(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), count(1) from {self.dbname}.meters')
        tdSql.checkResColNameList(['t1', 'c11', 't2', 'c21', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 6)
        tdSql.query(f'select cols(max(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), count(1) from {self.dbname}.meters where c0 < 4')
        tdSql.checkResColNameList(['t1', 'c11', 't2', 'c21', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929003)
        tdSql.checkData(0, 1, 3)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 5)
        tdSql.query(f'select cols(max(c0), ts as t123456789t123456789t123456789t123456789t123456789t123456789t123456789, c1 as c11), cols(first(c0), \
            ts as t123456789t123456789t123456789t123456789t123456789t123456789t123456789, c1 c21), count(1) from {self.dbname}.meters')
        tdSql.checkResColNameList(['t123456789t123456789t123456789t123456789t123456789t123456789t123', 'c11', \
            't123456789t123456789t123456789t123456789t123456789t123456789t123', 'c21', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 6)
        tdSql.query(f'select cols(max(c0), ts as t123456789t123456789t123456789t123456789t123456789t123456789t123456789, c1 as c11), cols(first(c0), \
            ts as t123456789t123456789t123456789t123456789t123456789t123456789t123456789, c1 c21), count(1) from {self.dbname}.meters where c0 < 4')
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929003)
        tdSql.checkData(0, 1, 3)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 5)
        tdSql.query(f'select cols(max(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), count(1) from test.meters where c0 < 4 group by tbname order by t1')
        tdSql.checkResColNameList(['t1', 'c11', 't2', 'c21', 'count(1)'])
        tdSql.checkRows(2)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 1)
        tdSql.checkData(1, 0, 1734574929003)
        tdSql.checkData(1, 1, 3)
        tdSql.checkData(1, 2, 1734574929000)
        tdSql.checkData(1, 3, 1)
        tdSql.checkData(1, 4, 4)

        tdSql.query(f'select cols(last_row(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), count(1) from test.meters where c0 < 4 group by tbname order by t1')
        tdSql.checkResColNameList(['t1', 'c11', 't2', 'c21', 'count(1)'])
        tdSql.checkRows(2)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 1)
        tdSql.checkData(1, 0, 1734574929003)
        tdSql.checkData(1, 1, 3)
        tdSql.checkData(1, 2, 1734574929000)
        tdSql.checkData(1, 3, 1)
        tdSql.checkData(1, 4, 4)
        
        tdSql.query(f'select cols(last_row(c0), ts as t1, c1 as c11), cols(min(c0), ts as t2, c1 c21), count(1) from test.meters where c0 < 4 group by tbname order by t1')
        tdSql.checkResColNameList(['t1', 'c11', 't2', 'c21', 'count(1)'])
        tdSql.checkRows(2)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 1)
        tdSql.checkData(1, 0, 1734574929003)
        tdSql.checkData(1, 1, 3)
        tdSql.checkData(1, 2, 1734574929000)
        tdSql.checkData(1, 3, 1)
        tdSql.checkData(1, 4, 4)
        
        tdSql.query(f'select cols(last_row(c0), ts as t1, c1 as c11), cols(mode(c0), ts as t2, c1 c21), count(1) from test.meters where c0 < 4 group by tbname order by t1')
        tdSql.checkResColNameList(['t1', 'c11', 't2', 'c21', 'count(1)'])
        tdSql.checkRows(2)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 1)
        tdSql.checkData(1, 0, 1734574929003)
        tdSql.checkData(1, 1, 3)
        #tdSql.checkData(1, 2, 1734574929000)    # mode(c0) is return a random ts of same c0 
        tdSql.checkData(1, 3, 2)
        tdSql.checkData(1, 4, 4)
        
        # fixed: has same select function outof cols func
        tdSql.query(f'select cols(last_row(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21),  first(c0) from test.meters where c0 < 4 group by tbname order by t1')
        tdSql.checkResColNameList(['t1', 'c11', 't2', 'c21', 'first(c0)'])
        tdSql.checkRows(2)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 1)
        tdSql.checkData(1, 0, 1734574929003)
        tdSql.checkData(1, 1, 3)
        tdSql.checkData(1, 2, 1734574929000)
        tdSql.checkData(1, 3, 1)
        tdSql.checkData(1, 4, 1)
        
        tdSql.query(f'select cols(last_row(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), first(c0), cols(last(c0), ts, c1)  from test.meters where c0 < 4 group by tbname order by t1;')
        tdSql.checkResColNameList(['t1', 'c11', 't2', 'c21', 'first(c0)', 'ts', 'c1'])
        tdSql.checkRows(2)
        tdSql.checkCols(7)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 1)
        tdSql.checkData(0, 5, 1734574929000)
        tdSql.checkData(0, 6, 1)
        tdSql.checkData(1, 0, 1734574929003)
        tdSql.checkData(1, 1, 3)
        tdSql.checkData(1, 2, 1734574929000)
        tdSql.checkData(1, 3, 1)
        tdSql.checkData(1, 4, 1)
        tdSql.checkData(1, 5, 1734574929003)
        tdSql.checkData(1, 6, 3)
        
        # sub query has cols func
        tdSql.query(f'select  c11 from (select cols(last_row(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), first(c0)  from test.meters where c0 < 4)')
        tdSql.checkRows(1)
        tdSql.checkCols(1)
        tdSql.checkData(0, 0, 3)
        tdSql.query(f'select  c11, c21 from (select cols(last_row(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), first(c0)  from test.meters where c0 < 4)')
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 3)
        tdSql.checkData(0, 1, 1)
        tdSql.query(f'select  c1, c21 from (select cols(last_row(c0), ts as t1, c1), cols(first(c0), ts as t2, c1 c21), first(c0)  from test.meters where c0 < 4)')
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 3)
        tdSql.checkData(0, 1, 1)
        tdSql.query(f'select  * from (select cols(last_row(c0), ts as t1, c1), cols(first(c0), ts as t2, c1 c21), first(c0)  from test.meters where c0 < 4)')
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 1, 3)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 1)
        tdSql.error(f'select  c1 from (select cols(last_row(c0), ts as t1, c1), cols(first(c0), ts as t2, c1), first(c0)  from test.meters where c0 < 4)')

        tdSql.query(f'select  c11, _rowts from (select cols(last_row(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), first(c0)  from test.meters where c0 < 4)')
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 3)
        tdSql.query(f'select  c11, c21, _rowts from (select cols(last_row(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), first(c0)  from test.meters where c0 < 4)')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 3)
        tdSql.checkData(0, 1, 1)
        tdSql.query(f'select  c1, c21, _rowts from (select cols(last_row(c0), ts as t1, c1), cols(first(c0), ts as t2, c1 c21), first(c0)  from test.meters where c0 < 4)')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 3)
        tdSql.checkData(0, 1, 1)
        tdSql.query(f'select  *, _rowts from (select cols(last_row(c0), ts as t1, c1), cols(first(c0), ts as t2, c1 c21), first(c0)  from test.meters where c0 < 4)')
        tdSql.checkRows(1)
        tdSql.checkCols(6)
        tdSql.checkData(0, 1, 3)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 1)
        tdSql.error(f'select  c1, _rowts from (select cols(last_row(c0), ts as t1, c1), cols(first(c0), ts as t2, c1), first(c0)  from test.meters where c0 < 4)')


        # cols on system table
        tdSql.query(f'select cols(max(vgroup_id), uid) from information_schema.ins_tables')
        tdSql.checkRows(1)
        tdSql.checkCols(1)
        tdSql.query(f'select cols(max(vgroup_id), uid, `ttl`, create_time) from information_schema.ins_tables')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.query(f'select cols(max(vgroup_id), uid as uidname) from information_schema.ins_tables')
        tdSql.checkRows(1)
        tdSql.checkCols(1)
        tdSql.error(f'select cols(last(vgroup_id), uid, `ttl`, create_time) from information_schema.ins_tables')
        tdSql.error(f'select cols(first(vgroup_id), uid, `ttl`, create_time) from information_schema.ins_tables')     

    def funcSupperTableTest(self, cachemodel):
        tdSql.execute(f'drop database if exists db;')
        tdSql.execute(f'create database if not exists db cachemodel \'{cachemodel}\';')
        tdSql.execute(f'use db')
        tdSql.execute(f'drop table if exists db.st')
        
        tdSql.execute('create table db.st (ts timestamp, c0 int, c1 float, c2 nchar(30), c3 bool) tags (t1 nchar(30))')
        tdSql.execute('create table db.st_1 using db.st tags("st1")')
        tdSql.execute('create table db.st_2 using db.st tags("st1")')
        tdSql.execute('insert into db.st_1 values(1734574929000, 1, 1, "c2", true)')
        tdSql.execute('insert into db.st_1 values(1734574929001, 2, 2, "bbbbbbbbb1", false)')
        tdSql.execute('insert into db.st_1 values(1734574929002, 3, 3, "a2", true)')
        tdSql.execute('insert into db.st_1 values(1734574929004, 4, 4, "bbbbbbbbb2", false)')
        
        tdSql.query(f'select cols(last(c0), ts, c1, c2, c3), cols(first(c0), ts, c1, c2, c3) from db.st')
        tdSql.checkRows(1)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, '4.0')
        tdSql.checkData(0, 2, 'bbbbbbbbb2')
        tdSql.checkData(0, 3, False)
        tdSql.checkData(0, 4, 1734574929000)
        tdSql.checkData(0, 5, '1.0')
        tdSql.checkData(0, 6, 'c2')
        tdSql.checkData(0, 7, True)
        
        tdSql.execute(f'drop table if exists db.st')

    
    def funcNestTest(self, cachemodel):
        tdSql.execute(f'drop database if exists db;')
        tdSql.execute(f'create database if not exists db cachemodel \'{cachemodel}\';')
        tdSql.execute(f'use db')
        tdSql.execute(f'drop table if exists db.d1')
        
        tdSql.execute('create table db.d1 (ts timestamp, c0 int, c1 float, c2 nchar(30), c3 bool)')
        tdSql.execute('insert into db.d1 values(1734574929000, 1, 1.1, "a", true)')
        tdSql.execute('insert into db.d1 values(1734574930000, 2, 2.2, "bbbbbbbbb", false)')
        
        groupby = ["", "group by tbname order ts", "group by tbname order t1", "group by tbname order ts"]
        
        tdSql.query(f'select cols(last(c0), ts, c2), cols(first(c0), ts, c2) from db.d1')
        tdSql.checkResColNameList(['ts', 'c2', 'ts', 'c2'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1734574930000)
        tdSql.checkData(0, 1, 'bbbbbbbbb')
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 'a')
        tdSql.query(f'select cols(last(c0), ts, c1, c2, c3), cols(first(c0), ts, c1, c2, c3) from db.d1')
        tdSql.checkResColNameList(['ts', 'c1', 'c2', 'c3', 'ts', 'c1', 'c2', 'c3'])
        tdSql.checkRows(1)
        tdSql.checkCols(8)
        tdSql.checkData(0, 0, 1734574930000)
        tdSql.checkData(0, 1, 2.2)
        tdSql.checkData(0, 2, 'bbbbbbbbb')
        tdSql.checkData(0, 3, False)
        tdSql.checkData(0, 4, 1734574929000)
        tdSql.checkData(0, 5, 1.1)
        tdSql.checkData(0, 6, 'a')
        tdSql.checkData(0, 7, True)
        
        tdSql.query(f'select cols(last(ts), c1), cols(first(ts), c1) from db.d1')
        tdSql.checkResColNameList(['c1', 'c1'])
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 2.2)
        tdSql.checkData(0, 1, 1.1)
        
        tdSql.query(f'select cols(first(ts), c0, c1), cols(first(ts), c0, c1) from db.d1')
        tdSql.checkResColNameList(['c0', 'c1', 'c0', 'c1'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1)
        tdSql.checkData(0, 1, 1.1)
        tdSql.checkData(0, 2, 1)
        tdSql.checkData(0, 3, 1.1)
        
        tdSql.query(f'select cols(first(ts), c0, c1), cols(first(ts+1), c0, c1) from db.d1')
        tdSql.checkResColNameList(['c0', 'c1', 'c0', 'c1'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1)
        tdSql.checkData(0, 1, 1.1)
        tdSql.checkData(0, 2, 1)
        tdSql.checkData(0, 3, 1.1)
        
        tdSql.query(f'select cols(first(ts), c0, c1), cols(first(ts), c0+1, c1+2) from db.d1')
        tdSql.checkResColNameList(['c0', 'c1', 'c0+1', 'c1+2'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1)
        tdSql.checkData(0, 1, 1.1)
        tdSql.checkData(0, 2, 2)
        tdSql.checkData(0, 3, 3.1)
        
        tdSql.query(f'select cols(first(c0), ts, length(c2)), cols(last(c0), ts, length(c2)) from db.d1')
        tdSql.checkResColNameList(['ts', 'length(c2)', 'ts', 'length(c2)'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 1734574930000)
        tdSql.checkData(0, 3, 36)
        tdSql.query(f'select cols(first(c0), ts, length(c2)), cols(last(c0), ts, length(c2) + 2) from db.d1')
        tdSql.checkResColNameList(['ts', 'length(c2)', 'ts', 'length(c2) + 2'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 1734574930000)
        tdSql.checkData(0, 3, 38)
        
        tdSql.query(f'select cols(first(c0), ts, c2), cols(last(c0), ts, length(c2) + 2) from db.d1')
        tdSql.checkResColNameList(['ts', 'c2', 'ts', 'length(c2) + 2'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 'a')
        tdSql.checkData(0, 2, 1734574930000)
        tdSql.checkData(0, 3, 38)
        
        tdSql.query(f'select cols(min(c0), ts, c2), cols(last(c0), ts, length(c2) + 2) from db.d1')
        tdSql.checkResColNameList(['ts', 'c2', 'ts', 'length(c2) + 2'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 'a')
        tdSql.checkData(0, 2, 1734574930000)
        tdSql.checkData(0, 3, 38)

        tdSql.query(f'select cols(min(c0), ts, c2), cols(first(c0), ts, length(c2) + 2) from db.d1')
        tdSql.checkResColNameList(['ts', 'c2', 'ts', 'length(c2) + 2'])
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 'a')
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 6)

    def orderby_test(self, from_table = 'test.meters', col_name='c0', isTmpTable = False):
        select_t1 = ["", ", t1", ", t1 as tag1"]
        for t1 in select_t1:
            if t1 != "" and isTmpTable:
                # Not a GROUP BY expression
                tdSql.error(f'select count(1), cols(last({col_name}),c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), c2)')  
                tdSql.error(f'select count(1), cols(last({col_name}),c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), c2) desc')  
                tdSql.error(f'select count(1), cols(last({col_name}),c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name})')  
                tdSql.error(f'select count(1), cols(last({col_name}),c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name}), cols(last({col_name}), c1)')  
                tdSql.error(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), c2)')  
                tdSql.error(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), c2) desc')  
                tdSql.error(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name})')  
                tdSql.error(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name}), cols(last({col_name}), c1)')  
                tdSql.error(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), c2)')  
                tdSql.error(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), c2) desc')  
                tdSql.error(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name})')  
                tdSql.error(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name}), cols(last({col_name}), c1)')
                continue
            tdSql.query(f'select count(1), cols(last({col_name}),c2) {t1}  from {from_table} group by tbname order by cols(last({col_name}), c2)')  
            tdSql.checkRows(2)
            tags_count = 0 if t1 == "" else 1
            tdLog.debug(f'tags_count: {tags_count}')
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 5)
            tdSql.checkData(0, 1, 'bbbbbbbbb2')
            self.condition_check(t1 != "", 0, 2, 'st1')
            tdSql.checkData(1, 0, 1)
            tdSql.checkData(1, 1, 'c2')
            self.condition_check(t1 != "", 1, 2, 'st2')
            tdSql.error(f'select count(1), cols(last({col_name}),*) {t1}  from {from_table} group by tbname order by cols(last({col_name}), *)') 
            
            #tdSql.query(f'select count(1), cols(last({col_name}),c2) {t1}  from {from_table} group by tbname order by cols(last({col_name}), *)')  

            tdSql.query(f'select count(1), cols(last({col_name}),c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), c2) desc')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'bbbbbbbbb2')
            self.condition_check(t1 != "", 1, 2, 'st1')

            tdSql.query(f'select count(1), cols(last({col_name}),c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name})')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'bbbbbbbbb2')
            self.condition_check(t1 != "", 1, 2, 'st1')
            
            tdSql.query(f'select count(1), cols(last({col_name}),c2)  {t1} from {from_table} group by tbname order by 1')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'bbbbbbbbb2')
            self.condition_check(t1 != "", 1, 2, 'st1')
            
            tdSql.query(f'select count(1), cols(last({col_name}),c2)  {t1} from {from_table} group by tbname order by 2')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 5)
            tdSql.checkData(0, 1, 'bbbbbbbbb2')
            self.condition_check(t1 != "", 0, 2, 'st1')
            tdSql.checkData(1, 0, 1)
            tdSql.checkData(1, 1, 'c2')
            self.condition_check(t1 != "", 1, 2, 'st2')

            tdSql.query(f'select count(1), cols(last({col_name}),c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name}), cols(last({col_name}), c1)')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'bbbbbbbbb2')
            self.condition_check(t1 != "", 1, 2, 'st1')

            tdSql.query(f'select count(1), last(c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), c2)')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 5)
            tdSql.checkData(0, 1, 'bbbbbbbbb2')
            self.condition_check(t1 != "", 0, 2, 'st1')
            tdSql.checkData(1, 0, 1)
            tdSql.checkData(1, 1, 'c2')
            self.condition_check(t1 != "", 1, 2, 'st2')

            tdSql.query(f'select count(1), last(c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), c2) desc')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'bbbbbbbbb2')
            self.condition_check(t1 != "", 1, 2, 'st1')

            tdSql.query(f'select count(1), last(c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name})')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'bbbbbbbbb2')
            self.condition_check(t1 != "", 1, 2, 'st1')

            tdSql.query(f'select count(1), last(c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name}), cols(last({col_name}), c1)')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'bbbbbbbbb2') 
            self.condition_check(t1 != "", 1, 2, 'st1')

            tdSql.query(f'select count(1), max(c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), c2)')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 5)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st1')
            tdSql.checkData(1, 0, 1)
            tdSql.checkData(1, 1, 'c2')
            self.condition_check(t1 != "", 1, 2, 'st2')
            
            tdSql.query(f'select count(1), max(c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), c2) desc')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'c2')
            self.condition_check(t1 != "", 1, 2, 'st1')

            tdSql.query(f'select count(1), max(c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name})')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'c2')
            self.condition_check(t1 != "", 1, 2, 'st1')

            tdSql.query(f'select count(1), max(c2)  {t1} from {from_table} group by tbname order by cols(last({col_name}), {col_name}), cols(last({col_name}), c1)')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 0, 1)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 0, 5)
            tdSql.checkData(1, 1, 'c2')
            self.condition_check(t1 != "", 1, 2, 'st1')
            
            tdSql.query(f'select _rowts, max(c2)  {t1}  from {from_table} group by tbname order by cols(last({col_name}), {col_name}), cols(last({col_name}), c1)')  
            tdSql.checkRows(2)
            tdSql.checkCols(2 + tags_count)
            tdSql.checkData(0, 1, 'c2')
            self.condition_check(t1 != "", 0, 2, 'st2')
            tdSql.checkData(1, 1, 'c2')
            self.condition_check(t1 != "", 1, 2, 'st1')          
          
    def parse_test(self):
        tdLog.info("parse test")
        
        #** error sql  **#
        tdSql.error(f'select cols(ts) from {self.dbname}.meters group by tbname')
        tdSql.error(f'select cols(ts) from {self.dbname}.meters')
        tdSql.error(f'select last(cols(ts)) from {self.dbname}.meters')
        tdSql.error(f'select last(cols(ts, ts)) from {self.dbname}.meters')
        tdSql.error(f'select last(cols(ts, ts), ts) from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts), ts as t1) as t1 from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts), ts, c0) t1 from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts), ts t1) tt from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts), c0 cc0, c1 cc1) cc from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts), c0 as cc0) as cc from {self.dbname}.meters')
        tdSql.error(f'select cols(ts) + 1 from {self.dbname}.meters group by tbname')
        tdSql.error(f'select last(cols(ts)+1) from {self.dbname}.meters')
        tdSql.error(f'select last(cols(ts+1, ts)) from {self.dbname}.meters')
        tdSql.error(f'select last(cols(ts, ts), ts+1) from {self.dbname}.meters')
        tdSql.error(f'select last(cols(last(ts+1), ts+1), ts) from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts), ts+1 as t1) as t1 from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts+1), ts, c0) t1 from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts), ts t1) tt from {self.dbname}.meters')
        tdSql.error(f'select cols(first(ts+1), c0+2 cc0, c1 cc1) cc from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts)+1, c0+2 as cc0) as cc from {self.dbname}.meters')
        tdSql.error(f'select cols(ABS(c0), c1) from {self.dbname}.meters group by tbname')
        tdSql.error(f'select cols(top(c0, 5), c1) from {self.dbname}.meters')
        tdSql.error(f'select cols(tail(c0, 5),c1) from {self.dbname}.meters')
        tdSql.error(f'select cols(BOTTOM(c0, 5),c1) from {self.dbname}.meters')
        tdSql.error(f'select cols(tail(ts, 5),c1) from {self.dbname}.meters')
        tdSql.error(f'select cols(UNIQUE(ts),c1) from {self.dbname}.meters')
        tdSql.error(f'select cols(sample(ts, 5),c1) from {self.dbname}.meters')
        
        tdSql.error(f'select cols(last(ts)+1, ts) from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts)+10, c1+10) from {self.dbname}.meters group by tbname')
        
        tdSql.error(f'select cols(cols(last(ts), c0), c0) as cc from {self.dbname}.meters')
        tdSql.error(f'select cols(last(ts), cols(last(ts), c0), c0) as cc from {self.dbname}.meters')
  
        # Aggregate functions do not support nesting      
        tdSql.error(f'select count(1), cols(last_row(ts), ts, first(c0)), last_row(c1) from {self.dbname}.meters')
        
        # Not a GROUP BY expression
        tdSql.error(f'select count(1), cols(last(c0),c0)  from test.meters group by tbname order by c3 desc')

    def subquery_test(self):
        tdSql.query(f'select count(1), cols(last(c0),c0)  from (select * from test.d0)')
        tdSql.query(f'select count(1), cols(last(c0),c0)  from (select *, tbname from test.meters) group by tbname')
        
        tdSql.query(f'select last(mts)  from (select max(c0) as mc0, cols(max(c0), ts) as mts from test.meters group by tbname)')
        tdSql.checkRows(1)
        tdSql.checkCols(1)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.query(f'select max(mc0) as max_c0, cols(max(mc0), mts) max_ts  from (select max(c0) as mc0, cols(max(c0), ts) as mts from test.meters group by tbname)')
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 4)
        tdSql.checkData(0, 1, 1734574929004)
        
        tdSql.error(f'select last(ts)  from (select max(c0) as mc0, cols(max(c0), ts) as mts from test.meters group by tbname)')
        tdSql.error(f'select max(mc0) as max_c0, cols(max(mc0), ts) max_ts  from (select max(c0) as mc0, cols(max(c0), ts) as mts from test.meters group by tbname)')
        
        tdLog.info("subquery_test: orderby_test from meters")
        self.orderby_test("test.meters", "c0", False)
        tdLog.info("subquery_test: orderby_test from (select *, tbname from meters)")
        self.orderby_test("(select *, tbname from test.meters)", "c0", True)
        tdLog.info("subquery_test: one_cols_multi_output_with_group_test from meters")
        self.one_cols_multi_output_with_group_test("test.meters", False)
        tdLog.info("subquery_test: one_cols_multi_output_with_group_test from (select *, tbname from meters)")
        self.one_cols_multi_output_with_group_test("(select *, tbname from test.meters)", True)
        
        self.one_cols_multi_output_test("test.meters")
        self.one_cols_multi_output_test("(select *, tbname from test.meters)")

    def window_test(self):
        tdSql.query(f'select tbname, _wstart,_wend, max(c0), max(c1), cols( max(c0), c1)  from test.meters partition \
                    by tbname  count_window(2) order by tbname')
        tdSql.checkRows(4)
        tdSql.checkCols(6)
        tdSql.checkData(0, 0, 'd0')
        tdSql.checkData(3, 0, 'd1')
        tdSql.checkData(3, 5, 1)
        tdSql.query(f'select  _wstart,_wend, max(c0), max(c1), cols( max(c0), c1)  from test.d1 count_window(2);')
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 4, 1)
        tdSql.query(f'select  _wstart,_wend, max(c0), max(c1), cols( max(c0), c1)  from test.normal_table count_window(2);')
        tdSql.checkRows(3)
        tdSql.checkCols(5)
        tdSql.checkData(0, 4, 2)
        
        tdSql.query(f'select c0,  cols(first(ts), c0, c1) from test.meters state_window(c0) order by c0')
        tdSql.checkRows(4)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1)
        tdSql.checkData(1, 0, 2)
        tdSql.checkData(1, 1, 2)
        tdSql.checkData(1, 2, 2)
        tdSql.checkData(2, 0, 3)

        tdSql.query(f'select c0,  cols(first(ts), c0, c1), tbname from test.meters state_window(c0)')
        tdSql.checkRows(4)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1)
        tdSql.checkData(0, 3, 'd0')
        tdSql.checkData(1, 0, 2)
        tdSql.checkData(1, 1, 2)
        tdSql.checkData(1, 2, 2)
        tdSql.checkData(2, 0, 3)
        
        tdSql.query(f'select c0,  cols(first(ts), c0, c1), t1 from test.meters state_window(c0)')
        tdSql.checkRows(4)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1)
        tdSql.checkData(0, 3, 'st1')
        tdSql.checkData(1, 0, 2)
        tdSql.checkData(1, 1, 2)
        tdSql.checkData(1, 2, 2)
        tdSql.checkData(2, 0, 3)
        
        tdSql.error(f'select tbname, c0,  cols(first(ts), c0, c1), cols(last(ts), c0) from test.meters state_window(c0)')
    
    def window_test2(self):       
        tdSql.query(f'select  cols(first(ts), c0, c1),  count(1) from test.meters session(ts, 3)')
        tdSql.checkRows(2)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 1)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 6)
        tdSql.checkData(1, 0, 0)
        tdSql.checkData(1, 1, None)
        tdSql.checkData(1, 2, 5)
        
        tdSql.query(f'select  cols(max(c0), c1),  count(1) from test.meters session(ts, 3)')
        tdSql.checkRows(2)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 4)
        tdSql.checkData(0, 1, 6)
        tdSql.checkData(1, 0, None)
        tdSql.checkData(1, 1, 5)
        
        tdSql.query(f'select  cols(first(ts), c0, c1), first(ts),  count(1) from test.meters session(ts, 3)')
        tdSql.checkRows(2)
        tdSql.checkCols(4)
        
        tdSql.query(f'select  cols(first(ts), c0, c1), first(ts),  count(1) from test.meters event_window start with c3=true end with c3!=true')
        tdSql.checkRows(2)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1)
        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 3)
        tdSql.checkData(1, 0, 3)
        tdSql.checkData(1, 1, 3)
        tdSql.checkData(1, 2, 1734574929003)
        tdSql.checkData(1, 3, 2)
        
        tdSql.query(f'select   first(ts),  count(1) from test.meters event_window start with c3=true end with c3!=true')
        tdSql.checkRows(2)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 1734574929000)
        tdSql.checkData(0, 1, 3)
        tdSql.checkData(1, 0, 1734574929003)
        tdSql.checkData(1, 1, 2)
        

    def join_test(self):
        tdSql.query(f'select cols(last(a.ts), a.c0) from test.d0 a join test.d1 b on a.ts = b.ts;')
        tdSql.checkRows(1)
        tdSql.checkCols(1)
        tdSql.checkData(0, 0, 1)
        tdSql.query(f'select cols(last(a.ts), a.c0) from test.d0 a join test.d1 b on a.ts = b.ts and a.c0 = b.c0;')
        tdSql.checkRows(1)
        tdSql.checkCols(1)
        tdSql.checkData(0, 0, 1)  
        tdSql.query(f'select cols(last(a.ts), a.c0) from test.d0 a join test.d1 b on a.ts = b.ts and a.c0 > b.c0;')
        tdSql.checkRows(0)
        tdSql.query(f'select tbname, ts, c0 from (select cols(last(a.ts), a.tbname, a.ts, a.c0) from test.d0 a join test.d1 b on a.ts = b.ts and a.c0=b.c0)')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 'd0')
        tdSql.checkData(0, 1, 1734574929000)
        tdSql.checkData(0, 2, 1)      
        tdSql.query(f'select tbname, ts, c0 from (select cols(first(a.ts), a.tbname, a.ts, a.c0) from test.d0 a join test.d1 b on a.ts = b.ts and a.c0=b.c0)')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 'd0')
        tdSql.checkData(0, 1, 1734574929000)
        tdSql.checkData(0, 2, 1)
        tdSql.error(f'select tbname, ts, c0 from (select cols(first(a.ts), a.tbname, a.ts, a.c0), cols(last(a.ts), b.tbname, b.ts, b.c0) from test.d0 a join test.d1 b on a.ts = b.ts and a.c0=b.c0)')
        tdSql.query(f'select tbname, ts, c0 from (select cols(first(a.ts), a.tbname, a.ts, a.c0), cols(last(a.ts), b.tbname tbname1, b.ts ts2, b.c0 c02) from test.d0 a join test.d1 b on a.ts = b.ts and a.c0=b.c0)')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 'd0')
        tdSql.checkData(0, 1, 1734574929000)
        tdSql.checkData(0, 2, 1)
    
    def stream_cols_test(self):
        tdSql.execute(f'use {self.dbname}')
        streams: list[StreamItem] = []
        stream = StreamItem(
            id=0,
            stream=f'CREATE STREAM {self.dbname}.last_col_s1 INTERVAL(1s) SLIDING(1s) \
                      FROM {self.dbname}.meters PARTITION BY tbname STREAM_OPTIONS(FILL_HISTORY) INTO {self.dbname}.last_col1 \
                      AS SELECT _twstart wstart, _twend wend, cols(last(ts), ts, c0) from %%tbname where _c0 >= _twstart and _c0 < _twend;',
            res_query='select wstart, wend, ts, c0 from last_col1 where tag_tbname = \'d0\'',
            exp_query='select _wstart wstart, _wend wend, cols(last(ts), ts, c0) from d0 INTERVAL(1s) SLIDING(1s) limit 4;',
        )
        streams.append(stream)

        stream = StreamItem(
            id=1,
            stream=f'CREATE STREAM {self.dbname}.last_col_s2 INTERVAL(1s) SLIDING(1s) \
                      FROM {self.dbname}.meters PARTITION BY tbname STREAM_OPTIONS(FILL_HISTORY) INTO {self.dbname}.last_col2 \
                      AS SELECT _twstart wstart, _twend wend, last_row(ts) lts, cols(last(c0), ts as ts0), cols(last(c1), ts as ts1) from %%tbname where _c0 >= _twstart and _c0 < _twend;',
            res_query='select wstart, wend, lts, ts0, ts1 from last_col2 where tag_tbname = \'d0\'',
            exp_query='select _wstart wstart, _wend wend, last_row(ts) lts, cols(last(c0), ts as ts0), cols(last(c1), ts as ts1) from d0 INTERVAL(1s) SLIDING(1s) limit 4;',
        )
        streams.append(stream)

        for s in streams:
            s.createStream()
        tdStream.checkStreamStatus()

        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574930000, 0, 1, NULL, NULL)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574931000, 1, 1, NULL, NULL)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574932000, 2, 2, NULL, NULL)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574933000, 3, 3, NULL, NULL)')

        for s in streams:
            s.checkResults()
        
    def include_null_test(self):
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929010, 0, NULL, NULL, NULL)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929011, NULL, 1, NULL, NULL)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929012, NULL, NULL, 2, NULL)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929013, NULL, NULL, NULL, false)')
        tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929014, NULL, NULL, NULL, NULL)')
        
        tdSql.query(f'select cols(last(c0), ts), cols(last(c1), ts), cols(last(c2), ts), cols(last(c3), ts) from {self.dbname}.d0')
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 1734574929010)
        tdSql.checkData(0, 1, 1734574929011)
        tdSql.checkData(0, 2, 1734574929012)
        tdSql.checkData(0, 3, 1734574929013)
       
    def long_column_name_test(self):
        tdSql.execute(f'use {self.dbname}')     
        tdSql.execute(f'create table {self.dbname}.long_col_test (ts timestamp, longcolumntestlongcolumntestlongcolumntestlongcolumntest88888888 int, c1 float, c2 nchar(30), c3 bool) tags (t1 nchar(30))')
        tdSql.execute(f'create table {self.dbname}.dl0 using {self.dbname}.long_col_test tags("st1")')
        tdSql.execute(f'create table {self.dbname}.dl1 using {self.dbname}.long_col_test tags("st2")')
        tdSql.execute(f'insert into {self.dbname}.dl0 values(1734574929000, 1, 1, "c2", true)')
        tdSql.execute(f'insert into {self.dbname}.dl0 values(1734574929001, 2, 2, "bbbbbbbbb1", false)')
        tdSql.execute(f'insert into {self.dbname}.dl0 values(1734574929002, 2, 2, "bbbbbbbbb1", false)')
        tdSql.execute(f'insert into {self.dbname}.dl0 values(1734574929003, 3, 3, "a2", true)')
        tdSql.execute(f'insert into {self.dbname}.dl0 values(1734574929004, 4, 4, "bbbbbbbbb2", false)')
        
        tdSql.execute(f'insert into {self.dbname}.dl1 values(1734574929000, 1, 1, "c2", true)')
    
        tdSql.query(f'select cols(last(longcolumntestlongcolumntestlongcolumntestlongcolumntest88888888),ts lts, c1 lc1), cols(first(longcolumntestlongcolumntestlongcolumntestlongcolumntest88888888), ts fts, c1 as fc1), count(1) from test.long_col_test')
        tdSql.checkResColNameList(['lts', 'lc1', 'fts', 'fc1', 'count(1)'])
        tdSql.checkRows(1)
        tdSql.checkCols(5)
        tdSql.checkData(0, 0, 1734574929004)
        tdSql.checkData(0, 1, 4)
        tdSql.checkData(0, 2, 1734574929000)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 6)
        
        tdLog.info("long_column_name_test subquery_test: orderby_test from meters")
        self.orderby_test("test.long_col_test", "longcolumntestlongcolumntestlongcolumntestlongcolumntest88888888", False)
        tdLog.info("long_column_name_test subquery_test: orderby_test from (select *, tbname from meters)")
        self.orderby_test("(select *, tbname from test.long_col_test)", "longcolumntestlongcolumntestlongcolumntestlongcolumntest88888888", True)
        tdLog.info("long_column_name_test subquery_test: one_cols_multi_output_with_group_test from meters")
                
    def check_in_interval(self, cachemodel):
        dbname = "db1"
        tdSql.execute(f"drop database if exists {dbname} ")
        tdSql.execute(f"create database {dbname} cachemodel '{cachemodel}' vgroups 6")
        tdSql.execute(f"use {dbname}")

        tdSql.execute(f" create stable {dbname}.sta (ts timestamp, f1 int, f2 binary(10), f3 bool) tags(t1 int, t2 bool, t3 binary(10));")
        tdSql.execute(f" create table {dbname}.tba1 using sta tags(0, false, '0');")
        tdSql.execute(f" create table {dbname}.tba2 using sta tags(1, true, '1');")
        tdSql.execute(f" create table {dbname}.tba3 using sta tags(null, null, '');")
        tdSql.execute(f" create table {dbname}.tba4 using sta tags(1, false, null);")
        tdSql.execute(f" create table {dbname}.tba5 using sta tags(3, true, 'aa');")
        
        tdSql.execute(f"insert into {dbname}.tba1 values ('2022-09-26 15:15:01', 0, 'a', false);")
        tdSql.execute(f"insert into {dbname}.tba1 values ('2022-09-26 15:15:02', 1, '0', true);")
        tdSql.execute(f"insert into {dbname}.tba1 values ('2022-09-26 15:15:03', 5, '5', false);")
        tdSql.execute(f"insert into {dbname}.tba1 values ('2022-09-26 15:15:04', null, null, null);")

        tdSql.execute(f"insert into {dbname}.tba2 values ('2022-09-27 15:15:01', 0, 'a', false);")
        tdSql.execute(f"insert into {dbname}.tba2 values ('2022-09-27 15:15:02', 1, '0', true);")
        tdSql.execute(f"insert into {dbname}.tba2 values ('2022-09-27 15:15:03', 5, '5', false);")
        tdSql.execute(f"insert into {dbname}.tba2 values ('2022-09-27 15:15:04', null, null, null);")

        tdSql.execute(f"insert into {dbname}.tba3 values ('2022-09-28 15:15:01', 0, 'a', false);")
        tdSql.execute(f"insert into {dbname}.tba3 values ('2022-09-28 15:15:02', 1, '0', true);")
        tdSql.execute(f"insert into {dbname}.tba3 values ('2022-09-28 15:15:03', 5, '5', false);")
        tdSql.execute(f"insert into {dbname}.tba3 values ('2022-09-28 15:15:04', null, null, null);")

        tdSql.execute(f"insert into {dbname}.tba4 values ('2022-09-29 15:15:01', 0, 'a', false);")
        tdSql.execute(f"insert into {dbname}.tba4 values ('2022-09-29 15:15:02', 1, '0', true);")
        tdSql.execute(f"insert into {dbname}.tba4 values ('2022-09-29 15:15:03', 5, '5', false);")
        tdSql.execute(f"insert into {dbname}.tba4 values ('2022-09-29 15:15:04', null, null, null);")

        tdSql.execute(f"insert into {dbname}.tba5 values ('2022-09-30 15:15:01', 0, 'a', false);")
        tdSql.execute(f"insert into {dbname}.tba5 values ('2022-09-30 15:15:02', 1, '0', true);")
        tdSql.execute(f"insert into {dbname}.tba5 values ('2022-09-30 15:15:03', 5, '5', false);")
        tdSql.execute(f"insert into {dbname}.tba5 values ('2022-09-30 15:15:04', null, null, null);")
        
        tdSql.query(f'select tbname, cols(last(ts), ts as ts1), cols(last(f1), ts as ts2) from tba5 interval(1s);')
        tdSql.checkRows(4)
        tdSql.checkCols(3)
        tdSql.checkData(0, 1, "2022-09-30 15:15:01")
        tdSql.checkData(0, 2, "2022-09-30 15:15:01")
        tdSql.checkData(1, 1, "2022-09-30 15:15:02")
        tdSql.checkData(1, 2, "2022-09-30 15:15:02")
        tdSql.checkData(2, 1, "2022-09-30 15:15:03")
        tdSql.checkData(2, 2, "2022-09-30 15:15:03")
        tdSql.checkData(3, 1, "2022-09-30 15:15:04")
        tdSql.checkData(3, 2, None)
        
        tdSql.query(f'select tbname, last(f1), ts from tba5 interval(1s)')
        tdSql.checkRows(3)
        tdSql.checkCols(3)
        tdSql.checkData(0, 1, 0)
        tdSql.checkData(0, 2, "2022-09-30 15:15:01")
        tdSql.checkData(1, 1, 1)
        tdSql.checkData(1, 2, "2022-09-30 15:15:02")
        tdSql.checkData(2, 1, 5)
        tdSql.checkData(2, 2, "2022-09-30 15:15:03")
        
        tdSql.query(f'select tbname, last(ts) from tba5 interval(1s)')
        tdSql.checkData(0, 1, "2022-09-30 15:15:01")
        tdSql.checkData(1, 1, "2022-09-30 15:15:02")
        tdSql.checkData(2, 1, "2022-09-30 15:15:03")
        tdSql.checkData(3, 1, "2022-09-30 15:15:04")
        
        tdSql.query(f'select tbname, cols(last(ts), ts as ts1), cols(last(f1), ts as ts2) from {dbname}.sta group by tbname;')
        tdSql.checkRows(5)
        tdSql.checkCols(3)
        tdSql.query(f'select tbname, cols(last(ts), ts as ts1), cols(last(f1), ts as ts2) from {dbname}.sta group by tbname order by ts1;')
        tdSql.checkRows(5)
        tdSql.checkCols(3)
        tdSql.checkData(0, 1, "2022-09-26 15:15:04")
        tdSql.checkData(0, 2, "2022-09-26 15:15:03")
        tdSql.checkData(1, 1, "2022-09-27 15:15:04")
        tdSql.checkData(1, 2, "2022-09-27 15:15:03")
        tdSql.checkData(2, 1, "2022-09-28 15:15:04")
        tdSql.checkData(2, 2, "2022-09-28 15:15:03")
        tdSql.checkData(3, 1, "2022-09-29 15:15:04")
        tdSql.checkData(3, 2, "2022-09-29 15:15:03")
        tdSql.checkData(4, 1, "2022-09-30 15:15:04")
        tdSql.checkData(4, 2, "2022-09-30 15:15:03")

        tdSql.query(f'select t1, cols(last(ts), ts as ts1), cols(last(f1), ts as ts2) from {dbname}.sta group by t1;')
        tdSql.checkRows(4)
        tdSql.checkCols(3)
        tdSql.query(f'select t1, cols(last(ts), ts as ts1), cols(last(f1), ts as ts2) from {dbname}.sta group by t1 order by ts1;')
        tdSql.checkRows(4)
        tdSql.checkCols(3)
        tdSql.checkData(0, 1, "2022-09-26 15:15:04")
        tdSql.checkData(0, 2, "2022-09-26 15:15:03")
        tdSql.checkData(1, 1, "2022-09-28 15:15:04")
        tdSql.checkData(1, 2, "2022-09-28 15:15:03")
        tdSql.checkData(2, 1, "2022-09-29 15:15:04")
        tdSql.checkData(2, 2, "2022-09-29 15:15:03")
        tdSql.checkData(3, 1, "2022-09-30 15:15:04")
        tdSql.checkData(3, 2, "2022-09-30 15:15:03")
        
    def having_test(self, table_name, is_subquery):
        tdLog.info("having_test")
        t1 = f"from {table_name} "
        tdSql.query(f'select tbname, cols(last(ts), ts) {t1} group by tbname having cols(last(ts), ts) > 1734574929000')
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 'd0')
        tdSql.checkData(0, 1, 1734574929014)
        tdSql.query(f'select tbname, cols(last(ts), ts) {t1} group by tbname having cols(last(ts), ts) = 1734574929000')
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 'd1')
        tdSql.checkData(0, 1, 1734574929000)
        tdSql.query(f'select tbname, cols(last(ts), ts) {t1} group by tbname having cols(last(ts), ts) < 1734574929000')
        tdSql.checkRows(0)
        tdSql.query(f'select tbname, cols(last(ts), ts) {t1} group by tbname having cols(last(ts), ts) != 1734574929000')
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 'd0')
        tdSql.checkData(0, 1, 1734574929014)
        tdSql.query(f'select tbname, cols(last(ts), ts) {t1} group by tbname having cols(last(ts), ts) >= 1734574929000')
        tdSql.checkRows(2)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 'd1')
        tdSql.checkData(0, 1, 1734574929000)
        tdSql.checkData(1, 0, 'd0')
        tdSql.checkData(1, 1, 1734574929014)
        tdSql.query(f'select tbname, cols(last(ts), ts) {t1} group by tbname having cols(last(ts), ts) <= 1734574929000')
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 'd1')
        tdSql.checkData(0, 1, 1734574929000)
        tdSql.query(f'select tbname, cols(last(ts), ts) {t1} group by tbname having cols(last(c0), ts) between 1734574929000 and 1734574929014')
        tdSql.checkRows(2)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 'd1')
        tdSql.checkData(0, 1, 1734574929000)
        tdSql.checkData(1, 0, 'd0')
        tdSql.checkData(1, 1, 1734574929014)
 
    def star_test(self):
        tdLog.info("star_test")
        tdSql.query(f'select tbname, cols(last(ts), *) from test.meters group by tbname having cols(last(ts), ts) > 1734574929000')
        tdSql.checkRows(1)
        tdSql.checkCols(7)
        tdSql.checkData(0, 0, 'd0')
        tdSql.checkData(0, 1, 1734574929014)
        tdSql.query(f'select tbname, cols(last(ts), *) from test.meters group by tbname having cols(last(ts), ts) = 1734574929000')
        tdSql.checkRows(1)
        tdSql.checkCols(7)
        tdSql.checkData(0, 0, 'd1')
        tdSql.checkData(0, 1, 1734574929000)
        tdSql.checkData(0, 2, 1)
        tdSql.checkData(0, 3, 1)
        tdSql.checkData(0, 4, 'c2')
        tdSql.checkData(0, 5, True)
        tdSql.error(f'select tbname, cols(last(ts), *) from test.meters group by tbname having cols(last(ts), *) = 1734574929000')

        
    def check_null2(self, cachemodel):
        dbname = "test_null2"
        tdSql.execute(f"drop database if exists {dbname}")
        tdSql.execute(f"create database test_null2 cachemodel '{cachemodel}' vgroups 5")
        tdSql.execute(f"use test_null2")
        tdSql.execute(f"create stable {dbname}.stb_null1 (ts timestamp, c0 int, c1 int, c2 nchar(30), c3 bool) tags (t1 nchar(30))")
        tdSql.execute(f"create table {dbname}.sub_null_1 using {dbname}.stb_null1 tags('st1')")
        tdSql.execute(f"create table {dbname}.sub_null_2 using {dbname}.stb_null1 tags('st2')")
        
        tdSql.execute(f"insert into {dbname}.sub_null_1 values(1734574929000, 1, null, null, null)")
        tdSql.execute(f"insert into {dbname}.sub_null_1 values(1734574929001, 2, null, null, null)")
        
        tdSql.execute(f"insert into {dbname}.sub_null_2 values(1734574929000, 21, null, null, null)")
        tdSql.execute(f"insert into {dbname}.sub_null_2 values(1734574929001, 22, null, null, null)")
        
        tdSql.query(f'select cols(last(ts), ts), cols(last(c2), ts) from {dbname}.stb_null1')
        tdSql.checkRows(1)
        tdSql.checkCols(2)
        tdSql.checkData(0, 0, 1734574929001)
        tdSql.checkData(0, 1, None)
        
        tdSql.query(f'select tbname, cols(last(ts), c0), cols(last(c2), c0) from {dbname}.stb_null1 group by tbname order by tbname')
        tdSql.checkRows(2)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 'sub_null_1')
        tdSql.checkData(0, 1, 2)
        tdSql.checkData(0, 2, None)
        tdSql.checkData(1, 0, 'sub_null_2')
        tdSql.checkData(1, 1, 22)
        tdSql.checkData(1, 2, None)
        
        tdSql.query(f'select tbname, t1, cols(last(ts), c0), cols(last(c2), c0) from {dbname}.sub_null_1')
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 'sub_null_1')
        tdSql.checkData(0, 1, "st1")
        tdSql.checkData(0, 2, 2)
        tdSql.checkData(0, 3, None)
        
        tdSql.query(f'select tbname, t1, cols(last(ts), c0), cols(last(c2), ts) from {dbname}.sub_null_2')
        tdSql.checkRows(1)
        tdSql.checkCols(4)
        tdSql.checkData(0, 0, 'sub_null_2')
        tdSql.checkData(0, 1, "st2")
        tdSql.checkData(0, 2, 22)
        tdSql.checkData(0, 3, None)
  
        tdSql.query(f'select tbname, cols(last(ts), c0), cols(last(c2), c0) from {dbname}.sub_null_1')
        tdSql.checkRows(1)
        tdSql.checkCols(3)
        tdSql.checkData(0, 0, 'sub_null_1')
        tdSql.checkData(0, 1, 2)
        tdSql.checkData(0, 2, None)
        
        tdSql.error(f'select tbname, cols(last(ts), c0), cols(last(c2), c0) from {dbname}.stb_null1')
        tdSql.error(f'select t1, cols(last(ts), c0), cols(last(c2), c0) from {dbname}.stb_null1')
        
    def run_basic(self, cachemodel):
        self.funcNestTest(cachemodel)
        self.funcSupperTableTest(cachemodel)
        self.create_test_data(cachemodel)
        self.parse_test()
        self.one_cols_1output_test()
        self.multi_cols_output_test()
        self.subquery_test()
        self.window_test()
        self.join_test()
        self.check_in_interval(cachemodel)
        self.include_null_test()
        self.long_column_name_test()

        self.having_test("test.meters", False)
        self.having_test("(select tbname, * from test.meters)", True)
        self.star_test()
        self.check_null2(cachemodel)
        self.window_test2()
        self.stream_cols_test()

    #
    # ------------------ main ------------------
    #
    def test_func_select_cols(self):
        """ Fun: cols()

        1. Basic query for input different params
        2. Query on super/child/normal/empty table
        3. Support types
        4. Error cases
        5. Query with filter conditions
        6. Query with group/partition by/having/order by
        7. Query with tags
        8. Query with join/union/nest/interval/window
        9. Check null value
        10. Check single/multi output

        Since: v3.0.0.0

        Labels: common,ci

        Jira: None

        History:
            - 2025-9-29 Alex  Duan Migrated from uncatalog/system-test/2-query/test_cols_function.py

        """
        tdStream.createSnode()
        self.run_basic('none')
        self.run_basic('last_value')
        self.run_basic('last_row')
        self.run_basic('both')

    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)
